#!coding:utf-8
import json
from djcelery.models import PeriodicTask
from rest_framework import serializers
from rest_framework.fields import WritableField
from rest_framework.relations import PrimaryKeyRelatedField
from cvirt.manager.models import Server,Cluster,Instance,Storage,Network,BackupPolicy, FixedIp,\
    Template, Event, SecurityGroupIngressRule, Datacenter, Proposal, Setting, Bookmark, SecurityGroup, Interface, VDisk, TaskDetail, Snapshot, User,IscsiLun
from cvirt.common import utils
from cvirt.manager.models import CvirtPeriodicTask

'''
serializer建立级联关系，vm->host->cluster->datacenter
便于前台查询级联数据，而不用我们自己去组装了。
但是要注意的是，关联的serializer最好另外定义，只取我们需要的数据.
参考HostStatusSerializer
'''


class VMStatusSerializer(serializers.ModelSerializer):
    "用于级联查询,包含基本字段"
    class Meta:
        model = Instance
        fields = ('id','name','status')

class HostSerializer(serializers.ModelSerializer):
    instance_set = VMStatusSerializer(many=True)
    class Meta:
        model = Server
        fields = ('id','ip', 'host_name','password','port','cpu', 'mem', 'arch', 'os','hypervisor','memshare','cpu_type','powercard_address','qemu_version',
                    'powertool','cluster','status','instance_set','cpu_usage','mem_usage','gateway','dns1','dns2','address','netmask','usedhcp', 'powercard_username', 'powercard_password')
        depth = 1  #级联加载cluster

class HostStatusSerializer(serializers.ModelSerializer):
    "用于级联查询,包含基本字段"
    instance_set = VMStatusSerializer(many=True)
    class Meta:
        model = Server
        fields = ('id','host_name','status','instance_set','ip','mem','cpu_usage','mem_usage')

# class StorageStatusSerializer(serializers.ModelSerializer):
#     "用于级联查询,包含基本字段"
#     class Meta:
#         model = Storage
#         fields = ('id','name','type')

class StorageSerializer(serializers.ModelSerializer):

    class Meta:
        model = Storage
        fields = ('id','name','type','mount_point','status','host','path','username'
                  ,'password',"cluster",'totalsize','used','protocol','target')
        depth = 1

class ClusterStatusSerializer(serializers.ModelSerializer):
    "用于级联查询,包含基本字段"
    server_set = HostStatusSerializer(many=True)
    storage_set = StorageSerializer(many=True)
    class Meta:
        model = Cluster
        fields = ('id','name','server_set','storage_set')

class ClusterSerializer(serializers.ModelSerializer):
    server_set = HostStatusSerializer(many=True)
    storage_set = StorageSerializer(many=True)
    class Meta:
        model = Cluster
        fields = ('id', 'name', 'description', 'cpufamily','server_set','storage_set')


class DatacenterSerializer(serializers.ModelSerializer):
    cluster_set = ClusterStatusSerializer(many=True)
    class Meta:
        model = Datacenter
        fields = ('id', 'name','cluster_set')


class SettingSerializer(serializers.ModelSerializer):
    class Meta:
        model = Setting
        fields = ('name','value','description')

        

class NetworkSerializer(serializers.ModelSerializer):
    class ifaceid(serializers.ModelSerializer):
        class Meta:
            model = Interface
            fields = ('id',)
    interface_set = ifaceid(many=True)
    class Meta:
        model = Network
        depth = 1


class BackupstrategySerializer(serializers.ModelSerializer):
    class Meta:
        model = BackupPolicy
        depth = 1


class EventSerializer(serializers.ModelSerializer):
    class Meta:
        model = Event
        fields = ('name','desc','reason','status','created_at','id', 'start_time', 'end_time')

class ProposalSerializer(serializers.ModelSerializer):
    # storage = StorageSerializer()
    # network = NetworkSerializer()
    class Meta:
        model = Proposal
        depth=1


class VM_no_relation_serializer(serializers.ModelSerializer):
    host = HostSerializer()
    class Meta:
        model = Instance
        exclude = ('xml','deleted_at')


class IscsilunSerializer(serializers.ModelSerializer):
    class Meta:
        model = IscsiLun


class vdiskSerializer(serializers.ModelSerializer):
    instance = VM_no_relation_serializer()
    iscsilun = IscsilunSerializer()
    class Meta:
        model = VDisk
        fields = ('id','device_type','name','size','image','storage','instance',
                  'mountpoint','format','max_size','created_at','bus','cache','iomode','isopath','readonly','created_at','iscsilun')
        depth = 1

class TemplateSerializer(serializers.ModelSerializer):
    class vdiskid(serializers.ModelSerializer):
        class Meta:
            model = VDisk
            fields = ('id',)
    #因为现在一个虚拟机只能有一个磁盘是基于镜像生成的，所有template对应的vdisk数量就是对应的虚拟机数量
    vdisk_set = vdiskid(many=True)
    class Meta:
        model = Template
        fields = ('id','name','description','format','size','status','os_type','created_at','storage',
                  'virtualsize','vdisk_set','storagehost')
        depth = 1

class SecurityGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = SecurityGroup
        fields = ('id','name','description')

class FixedIpSerializer(serializers.ModelSerializer):
    class Meta:
        model = FixedIp
        depth = 1

class InterfaceSerializer(serializers.ModelSerializer):
    network = NetworkSerializer()
    securitygroups = SecurityGroupSerializer(many=True)
    fixedip = FixedIpSerializer()
    class Meta:
        model = Interface



class SnapshotSerializer(serializers.ModelSerializer):
    class Meta:
        model = Snapshot

class VMSerializer(serializers.ModelSerializer):

    host = HostSerializer()
    vdisk_set = vdiskSerializer(many=True)
    interface_set = InterfaceSerializer(many=True)
    snapshot_set = SnapshotSerializer(many=True)
    class Meta:
        model = Instance
        # fields = ('cpu_usage','mem_usage','name','id','uuid','description',
        #           'host','vcpu','memory','status','needha','os_type','power_status',
        #           'user','graphics_type','vdisk_set')
        exclude = ('xml','deleted_at')
        depth = 1

class GraphSerializer(serializers.Serializer):
    hostname = serializers.CharField(max_length=64)
    metric = serializers.CharField(blank=True)
    starttime = serializers.CharField(max_length=128,blank=True)
    endtime = serializers.CharField(max_length=128,blank=True)
    timerange = serializers.CharField(blank=True)


class BookmarkSerializer(serializers.ModelSerializer):
    class Meta:
        model = Bookmark


class CvirtPeriodicTaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = CvirtPeriodicTask
        fields = ('name', 'task', 'description', 'date_changed', 'expires', 'level', 'id')


class PeriodicTaskSerializer(serializers.ModelSerializer):
    class Meta:
        model = PeriodicTask
        fields = ('name', 'task', 'description', 'date_changed', 'expires', 'id', 'kwargs', 'queue')


class TaskDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = TaskDetail
        depth = 1

class FixedipSerializer(serializers.ModelSerializer):
    class Meta:
        model = FixedIp

class SecurityGroupIngressRuleSerializer(serializers.ModelSerializer):
    class Meta:
        model = SecurityGroupIngressRule
        depth=1


class UserSerializer(serializers.ModelSerializer):
    instance_set = VMStatusSerializer(many=True)
    class Meta:
        model = User
